import * as THREE from "three";
import { OrbitControls } from "three/addons/controls/OrbitControls.js";
import { GUI } from "three/addons/libs/lil-gui.module.min.js";
import { house } from "./house.js";
import grass from "./grass.js";
import { gui } from "./roof.js";

const scene = new THREE.Scene();
scene.fog = new THREE.Fog(0xcccccc, 1000, 40000);

const fogControl = gui.addFolder("雾");
fogControl.add(scene.fog, "near").step(100);
fogControl.add(scene.fog, "far").step(1000);

// 创建相机
const camera = new THREE.PerspectiveCamera(
	60,
	window.innerWidth / window.innerHeight,
	0.1,
	10000 // 相机的范围
);
camera.position.set(5000, 5000, 5000);
camera.lookAt(0, 0, 0);

// 创建渲染器
const renderer = new THREE.WebGLRenderer({
	logarithmicDepthBuffer: true, // 开启对数深度缓存
});
renderer.setClearColor(new THREE.Color("skyblue")); // 设置背景颜色

renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
// 渲染
let angle = 0;
let r = 5000;
function render() {
	angle += 0.01;

	if (angle >= Math.PI * 2) {
		angle -= Math.PI * 2;

		r = 5000 + Math.random() * 10000;

		camera.position.y = 1000 + Math.random() * 10000;
	}

	camera.position.x = r * Math.cos(angle);
	camera.position.z = r * Math.sin(angle);

	camera.lookAt(0, 0, 0);

	renderer.render(scene, camera);
	requestAnimationFrame(render);
}
render();

//添加坐标轴辅助器
const axesHelper = new THREE.AxesHelper(20000);
scene.add(axesHelper);

//轨道控制器
const controls = new OrbitControls(camera, renderer.domElement);
controls.addEventListener("change", function () {
	renderer.render(scene, camera);
});

const directionLight = new THREE.DirectionalLight(0xffffff);
directionLight.position.set(3000, 3000, 3000);
scene.add(directionLight);

const ambientLight = new THREE.AmbientLight();
scene.add(ambientLight);

scene.add(house);
scene.add(grass);
